package com.wb.base.refreshview;


import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.wb.base.R;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class RefreshListView extends FrameLayout implements ILoad, IRefresh {

    public LoadMoreRecyclerView loadMoreRecyclerView;
    private PtrHTFrameLayout store_house_ptr_frame;
    private HeaderView storeHouseHeader;
    private boolean isRefreshEnable = true;
    private OnRefreshListener onRefreshListener;
    private EmptyView emptyView;

    private boolean autoEnabledLoadmore = true;

    private long refreshTime;


    /**
     * 设置启动自动完成上拉加载更多
     *
     * @param autoEnabledLoadmore
     */
    public void setAutoEnabledLoadmore(boolean autoEnabledLoadmore) {
        this.autoEnabledLoadmore = autoEnabledLoadmore;
    }

    public RefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.refreshlistview, this);
        loadMoreRecyclerView = (LoadMoreRecyclerView) view.findViewById(R.id.loadMoreRecyclerView);
        loadMoreRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        store_house_ptr_frame = (PtrHTFrameLayout) view.findViewById(R.id.store_house_ptr_frame);
        emptyView = (EmptyView) findViewById(R.id.emptyView);
        emptyView.setContentView(loadMoreRecyclerView);
        emptyView.setOnRefreshListener(new EmptyView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (onRefreshListener != null) {
                    onRefreshListener.onRefresh();
                }
            }
        });

        initPtr();
    }


    private void initPtr() {

        storeHouseHeader = new HeaderView(getContext(), null);

        store_house_ptr_frame.autoRefresh(false);
        store_house_ptr_frame.setHeaderView(storeHouseHeader);
        store_house_ptr_frame.addPtrUIHandler(storeHouseHeader);

        store_house_ptr_frame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                if (isRefreshEnable) {
                    boolean canbePullDown = PtrDefaultHandler.checkContentCanBePulledDown(frame, loadMoreRecyclerView, header);
                    return canbePullDown;
                } else {
                    return false;
                }
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                refreshTime = System.currentTimeMillis();
                if (onRefreshListener != null) {
                    onRefreshListener.onRefresh();
                }
            }
        });

    }

    @Override
    public void enableLoadmore() {
        loadMoreRecyclerView.enableLoadmore();
    }

    @Override
    public void setOnLoadMoreListener(OnLoadMoreListener listener) {
        loadMoreRecyclerView.setOnLoadMoreListener(listener);
    }


    @Override
    public void disableLoadmore() {
        loadMoreRecyclerView.disableLoadmore();
    }

    @Override
    public void hideLoadMoreView() {
        loadMoreRecyclerView.hideLoadMoreView();
    }

    @Override
    public void completeLoadMore() {
        loadMoreRecyclerView.completeLoadMore();
    }

    @Override
    public void setAdapter(LoadRecyclerViewAdapter adapter) {
        loadMoreRecyclerView.setAdapter(adapter);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            store_house_ptr_frame.refreshComplete();
        }
    };

    @Override
    public void completeRefresh() {
        long ctime = System.currentTimeMillis();
        ctime = ctime - refreshTime;
        postDelayed(runnable, ctime > 1500 ? 1000 : 1500);
    }

    @Override
    public void isRefreshing(boolean isRefreshing) {
        store_house_ptr_frame.isRefreshing();
    }

    @Override
    public void setOnRefreshListener(OnRefreshListener listener) {
        this.onRefreshListener = listener;
    }

    @Override
    public void disableRefreshing() {
        isRefreshEnable = false;
    }

    @Override
    public void enableRefreshing() {
        isRefreshEnable = true;
    }

    public void setLayoutManager(LinearLayoutManager layoutManager) {
        loadMoreRecyclerView.setLayoutManager(layoutManager);
    }

    public LoadMoreRecyclerView getLoadMoreRecyclerView() {
        return loadMoreRecyclerView;
    }


    public void loading(boolean isRefresh) {
        LoadRecyclerViewAdapter adapter = (LoadRecyclerViewAdapter) loadMoreRecyclerView.getAdapter();
        int count = adapter != null ? adapter.getItemCount() : 0;
        emptyView.loading(count);
        View v = adapter.getLoadMoreView();
        if (v != null && v instanceof LoadView) {
            if (isRefresh) {
                ((LoadView) v).noData();
            } else
                ((LoadView) v).loading();
        }
        completeRefresh();
        if (autoEnabledLoadmore) {
            if (count > 0) {
                completeLoadMore();
            } else {
                disableLoadmore();
            }
        }

    }

    public void noNetError() {
        LoadRecyclerViewAdapter adapter = (LoadRecyclerViewAdapter) loadMoreRecyclerView.getAdapter();
        int count = adapter != null ? adapter.getItemCount() : 0;
        emptyView.noNetWork(count);
        View v = adapter.getLoadMoreView();
        if (v != null && v instanceof LoadView) {
            ((LoadView) v).noNetWork();
        }
        completeRefresh();
        if (autoEnabledLoadmore) {
            if (count > 0) {
                completeLoadMore();
            } else {
                disableLoadmore();
            }
        }

    }

    public void noData() {
        LoadRecyclerViewAdapter adapter = (LoadRecyclerViewAdapter) loadMoreRecyclerView.getAdapter();
        int count = adapter != null ? adapter.getItemCount() : 0;
        emptyView.noData(count);
        View v = adapter.getLoadMoreView();
        if (v != null && v instanceof LoadView) {
            ((LoadView) v).noData();
        }
        completeRefresh();
        disableLoadmore();
    }

    public void noDataForBind() {
        LoadRecyclerViewAdapter adapter = (LoadRecyclerViewAdapter) loadMoreRecyclerView.getAdapter();
        int count = adapter != null ? adapter.getItemCount() : 0;
        emptyView.noDataForBind(count);
        View v = adapter.getLoadMoreView();
        if (v != null && v instanceof LoadView) {
            ((LoadView) v).noData();
        }
        completeRefresh();
        disableLoadmore();
    }


    public RecyclerView.LayoutManager getLayoutManager() {
        if (null != loadMoreRecyclerView) {
            return loadMoreRecyclerView.getLayoutManager();
        }
        return null;
    }

    public void scrollTo(int positon) {
        if (null != loadMoreRecyclerView) {
            loadMoreRecyclerView.smoothScrollToPosition(positon);
        }
    }

    public View getEmptyView() {
        return emptyView;
    }


}
